/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manageBean;

import entidades.TablaTempCua;
import entidades.Ventas;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.model.chart.CartesianChartModel;  
import org.primefaces.model.chart.ChartSeries;  
import org.primefaces.model.chart.LineChartSeries;

/**
 *
 * @author hgmartinez
 */
@ManagedBean
@SessionScoped
public class MinimosCuadrados implements Serializable {
    private CartesianChartModel categoryModel; 
    private CartesianChartModel linearModel;

    private TablaTempCua temp;
    private ArrayList<TablaTempCua> tempS;
    private Ventas venta;
    private ArrayList<Ventas> ventas;
    private double xy;
    private double x2;
    private double sumatoriaX2, sumatoriaVentas, sumatoriaX, sumatoriaXy;
    private double a, b;
    private double proyectar;
    private double valoracionTotal;
    

    public MinimosCuadrados() {
        tempS = new ArrayList<>();
        temp = new TablaTempCua();
        venta = new Ventas();
        ventas = new ArrayList<>();
        a = 0.0;
        b = 0.0;
        xy = 0.0;
        x2 = 0.0;
        sumatoriaVentas = 0.0;
        sumatoriaX = 0.0;
        sumatoriaX2 = 0.0;
        sumatoriaXy = 0.0;
        proyectar=0.0;
        valoracionTotal=0.0;
        
    }
    private void createCategoryModel() {
        categoryModel = new CartesianChartModel();  
  
        ChartSeries a = new ChartSeries();  
        a.setLabel("Proyeccion");
        for (Iterator<Ventas> it = ventas.iterator(); it.hasNext();) {
            Ventas v = it.next();
            a.set(v.getMeses(), v.getValorazion());
        }
        categoryModel.addSeries(a);
        
    }
    public Ventas getVenta() {
        return venta;
    }

    public void setVenta(Ventas venta) {
        this.venta = venta;
    }

    public ArrayList<Ventas> getVentas() {
        return ventas;
    }

    public void setVentas(ArrayList<Ventas> ventas) {
        this.ventas = ventas;
    }

    public void procesarXY() {
        this.xy = venta.getMeses() * venta.getVentas();
        venta.setXy(xy);
    }

    public void procesarX2() {
        this.x2 = venta.getMeses() * venta.getMeses();
    }

    public void agregarVentas() {
        double val=0.0;
        
        try {
            for (Iterator<TablaTempCua> it = tempS.iterator(); it.hasNext();) {
                TablaTempCua t = it.next();
                venta = new Ventas();
                venta.setMeses(t.getNumero());
                venta.setVentas(t.getVentas());
                venta.setXy(t.getNumero() * t.getVentas());
                venta.setX2(t.getNumero() * t.getNumero());
                
                ventas.add(venta);
            }
        } catch (Exception e) {
            System.out.println("Se produjo  un erro al agregar la venta" + e.getMessage());
        } finally {
            this.reinit();
        }

    }

    public void reinit() {
        this.venta = new Ventas();
    }

    public double sumaXy() {
        for (Iterator<Ventas> it = ventas.iterator(); it.hasNext();) {
            Ventas v = it.next();
            this.sumatoriaXy += v.getVentas();
        }
        return this.sumatoriaXy;
    }

    public double sumaX() {
        for (Iterator<Ventas> it = ventas.iterator(); it.hasNext();) {
            Ventas v = it.next();
            this.sumatoriaX += v.getMeses();
        }
        return this.sumatoriaX;
    }

    public double sumaX2() {
        for (Iterator<Ventas> it = ventas.iterator(); it.hasNext();) {
            Ventas v = it.next();
            this.sumatoriaX2 += v.getX2();
        }
        return this.sumatoriaX2;
    }

    public double sumaVentas() {
        for (Iterator<Ventas> it = ventas.iterator(); it.hasNext();) {
            Ventas v = it.next();
            this.sumatoriaVentas += v.getVentas();
        }
        return this.sumatoriaVentas;
    }

    public void proyeccion() {
        int n = this.ventas.size();
        this.setProyectar(this.procesarA() + (this.procesarB() * (n + 1))); 
        this.createCategoryModel();
    }
    public void limpiarProyeccion(){
        this.setProyectar(0.0);
    }
    public void limpiarSumas(){
        this.sumatoriaX=0.0;
        this.sumatoriaX2=0.0;
        this.sumatoriaXy=0.0;
        this.sumatoriaVentas=0.0;
    }

    public double procesarA() {
        this.a = ((this.sumaVentas() * this.sumaX2()) - (this.sumaVentas() * this.sumaXy())) / (this.sumaX2() - Math.pow(this.sumaX2(), 2));
        return this.a;
    }

    public double procesarB() {
        this.b = (this.sumaXy() - (this.sumaX() * this.sumaX())) / (this.sumaX2() - Math.pow(this.sumaX2(), 2));
        return b;
    }

    public TablaTempCua getTemp() {
        return temp;
    }

    public void setTemp(TablaTempCua temp) {
        this.temp = temp;
    }

    public ArrayList<TablaTempCua> getTempS() {
        return tempS;
    }

    public void agregarTemp() {
        try {
            this.tempS.add(temp);
        } catch (Exception e) {
            System.out.println("Problemas pr a poer agregarlo");
        } finally {
            this.reinitT();
        }

    }

    public void reinitT() {
        this.temp = new TablaTempCua();
    }

    public double getProyectar() {
        return proyectar;
    }

    public void setProyectar(double proyectar) {
        this.proyectar = proyectar;
    }

    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }
    
}
